package cn.itcast.service;

import cn.itcast.OrdersettingDao;
import cn.itcast.OrdersettingService;
import cn.itcast.pojo.OrderSetting;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceImpl implements OrdersettingService {
    @Autowired
    OrdersettingDao ordersettingDao;

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Integer count = ordersettingDao.fingCountOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            ordersettingDao.editNumberByDate(orderSetting);
        } else {
            ordersettingDao.add(orderSetting);
        }


    }

    @Override
    public void addAll(ArrayList<OrderSetting> list) {
        if (list != null && list.size() > 0) {

            for (OrderSetting orderSetting : list) {
                Integer count = ordersettingDao.fingCountOrderDate(orderSetting.getOrderDate());
                if (count>0){
                    ordersettingDao.editNumberByDate(orderSetting);


                }else {
                    ordersettingDao.add(orderSetting);
                }
            }

        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin = date + "-1";//2019-3-1
        String dateEnd = date + "-31";//2019-3-31
        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        List<OrderSetting> list = ordersettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;




    }
}
